package org.paybiz.util;

public class PathUtil {
    // 获取当前文件路径
    public String getWebClassesPath() {
        String path = getClass().getProtectionDomain().getCodeSource()
                .getLocation().getPath();
        return path.substring(1);
    }

    // 获取当前工程的web-inf路径
    public String getWebInfPath() throws IllegalAccessException {
        String path = getWebClassesPath();
        if (path.indexOf("WEB-INF") > 0) {
            path = path.substring(0, path.indexOf("WEB-INF") + 8);
        } else {
            throw new IllegalAccessException("路径获取错误");
        }
        return path;
    }

    // 获取当前工程路径

    public String getWebRoot() throws IllegalAccessException {
        String path = getWebClassesPath();
        if (path.indexOf("WEB-INF") > 0) {
            path = path.substring(0, path.indexOf("WEB-INF/classes"));
        } else {
            throw new IllegalAccessException("路径获取错误");
        }
        return path;
    }

    /**
     * 获取配置WebInfo/classes下的配置文件
     * @param fileName 配置文件名称
     * @return
     * @throws IllegalAccessException
     */
    public String getWebInfoClassesFile(String fileName)
            throws IllegalAccessException {
        String path = "/" + getWebInfPath() + "classes/" + fileName;
        return path;
    }

    public static void main(String[] args) throws IllegalAccessException {
        PathUtil pathUtil = new PathUtil();
        System.out.println(pathUtil.getWebClassesPath());
        System.out.println(pathUtil.getWebInfPath());
    }
}
